# coding=UTF-8
from django.conf.urls.defaults import *
from blog.feeds import LatestEntries, LatestEntriesByCategory, PostEntry
#from django.views.decorators.cache import cache_page
from blog.views import list_post, view_post,view_post_perma
from django.views.generic.simple import redirect_to, direct_to_template
from django.views.generic import list_detail, date_based, create_update  
from django.views.generic.simple import direct_to_template

from google.appengine.api import users
from django.utils.encoding import force_unicode,smart_str
from blog.models import Post, Comment, Event,Tag,Category
import urllib
import upload
import settings

urlpatterns = patterns('blog.views',
    url(r'^$', 'list_post',name='list_post'),
    (r'^(?P<page>\d+)$', 'list_post',),
    (r'^post/', include('blog.urls')),    
    url(r'^category/(?P<category>.+)/(?P<page>\d+)$', 'category',name='category'),
    (r'^user/logout/$','logout'),
    (r'^user/login/$','login'),    
    (r'^event/$', 'event'),
    (r'^import/$', 'import_wxp'),
    (r'^export/$', 'export_wxp'),
)


urlpatterns += patterns('',

#    url(r'^(?P<year>\d+)/(?P<month>\d\d+)/(?P<slug>.*).html$', view_post_perma,name='view_post_perma'),
    (r'^about/$', direct_to_template, {'template': 'about.html','extra_context':{'users':users,},}),
    (r'^guest/$',direct_to_template, {'template': 'guestbook.html','extra_context':{'users':users,},}),
    (r'^tags/$',direct_to_template, {'template': 'tags.html','extra_context':{'users':users,'tags':Tag.all(),'categories':Category.all()},}),    
    (r'^labs/$',direct_to_template, {'template': 'labs.html','extra_context':{'users':users,'events':Event.all().order('-created')},}),    
    (r'^canvas.html$',direct_to_template, {'template': 'canvas.html','extra_context':{'users':users,},}),
    (r'^rpc_relay.html$',direct_to_template, {'template': 'rpc_relay.html','extra_context':{'users':users,},}),      
)

urlpatterns += patterns('upload.views',
    (r'^album/', include('upload.urls')),   
)

feeds={
       'latest':LatestEntries,
       'categories':LatestEntriesByCategory,
       'post':PostEntry,
}

urlpatterns += patterns('',
    (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict':feeds, }),
)

set=Post.objects.all().order('-date')
try :
    set._clone()
except AttributeError :    
    set._clone=lambda:set
    
try :
    set.model
except AttributeError :    
    set.model=Post
    
post_list_info = {  
    'queryset' : set,  
    'allow_empty': True,  
    'paginate_by':6,
    'extra_context':{'users':users,},
}

post_detail_info = {  
    "queryset" : set,  
    "template_object_name" : "post",  
} 

urlpatterns += patterns('',  
    (r'^posts/$', list_detail.object_list, post_list_info),                   
    (r'^posts/(?P<page>\d+)$', list_detail.object_list, post_list_info),
    (r'^posts/view/(?P<object_id>\d+)/$', list_detail.object_detail, post_detail_info),  

)

try:
    urlpatterns += patterns('',
        url(settings.LINK_FORMAT, view_post_perma,name='view_post_perma'),
    )
except:
    urlpatterns += patterns('',
        url(r'^(?P<year>\d+)/(?P<month>\d\d+)/(?P<slug>.*).html$', view_post_perma,name='view_post_perma'),
    )
